package com.example.hospitalbakcends.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospitalbakcends.mapper.DoctorMapper;
import com.example.hospitalbakcends.service.DoctorService;
import com.example.hospitalbakcends.domain.Doctor;
import com.example.hospitalbakcends.utils.CreateAccount;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.example.hospitalbakcends.constant.DoctorConstant.USER_LOGIN_STATE;

/**
* @author cac_qin
* @description 针对表【doctor(医生)】的数据库操作Service实现
* @createDate 2024-05-21 23:42:00
*/
@Service
@Slf4j
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor>
    implements DoctorService{
    @Resource
    private DoctorMapper doctorMapper;

    //加密
//    private static final String SALT="cac";
    @Override
    public List<Doctor> Bydepartmentlist(String department){
        QueryWrapper<Doctor> queryWrapper =new QueryWrapper<>();
        queryWrapper.select("doctorName","doctorId").eq("doctorDepartment",department);
        return doctorMapper.selectList(queryWrapper);
    }

    @Override
    public String doctorRegister(String doctorName, String doctorPassword, String doctorCheckPwd,String department){
        log.info("23123");
        if(StringUtils.isAnyBlank(doctorName,doctorPassword,doctorCheckPwd)){
            log.info("1");
            return "账号或者密码为空";
        }
        if(doctorName.length()<2){
            return  "名字长度不少于两位";
        }
        if(doctorPassword.length()<8 || doctorCheckPwd.length()<8){
            return "密码不少于8位";
        }
        log.info("2");
        //客户不能包含特殊字符
        String validPatten="^.*[/^/$/.//,;:'!@#%&/*/|/?/+/(/)/[/]/{/}]+.*$";
        Matcher matcher = Pattern.compile(validPatten).matcher(doctorName);
        if(matcher.find()){
            return "名字含特殊字符";
        }
        if(!doctorPassword.equals(doctorCheckPwd)){
            return "两次密码不匹配";
        }
        log.info("3");
        QueryWrapper<Doctor> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("doctorName",doctorName);
        int count = Math.toIntExact(doctorMapper.selectCount(queryWrapper));
        if(count>0){
            return "该姓名已注册";
        }
        //对密码进行加密
//        String encryptPassword = DigestUtils.md5DigestAsHex((SALT+userPwd).getBytes());
        log.info("4");
        //插入数据
        Doctor doctor =new Doctor();
        doctor.setDoctorname(doctorName);
        doctor.setDoctorpassword(doctorPassword);
        doctor.setDoctordepartment(department);
        boolean saveResult = this.save(doctor);
        if(!saveResult){
            return "插入失败，请联系管理员";
        }
        String  newAccount = CreateAccount.newAccount(doctor.getDoctorid());
        UpdateWrapper<Doctor> update = new UpdateWrapper<>();
        update.eq("doctorid",doctor.getDoctorid()).set("doctorAccount",newAccount);
        doctorMapper.update(doctor,update);
        return "你的账号是："+newAccount;
    }

    @Override
    public Doctor doctorLogin(String doctorAccount, String doctorPassword, HttpServletRequest httpServletRequest) {
        if(StringUtils.isAnyBlank(doctorAccount, doctorPassword)){
            return null;
        }
        if(doctorAccount.length()<2){
            return null;
        }
        if(doctorPassword.length()<8 ){
            return null;
        }

        //账号输入不能包含特殊字符
        String validPatten="^.*[/^/$/.//,;:'!@#%&/*/|/?/+/(/)/[/]/{/}]+.*$";
        Matcher matcher = Pattern.compile(validPatten).matcher(doctorAccount);
        if(matcher.find()){
            return null;
        }

        //对密码进行加密
//        String encryptPassword = DigestUtils.md5DigestAsHex((SALT+userPwd).getBytes());

        //查询数据库
        QueryWrapper<Doctor> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("doctorAccount", doctorAccount);
        queryWrapper.eq("doctorPassword", doctorPassword);
        Doctor doctor = doctorMapper.selectOne(queryWrapper);

        //医生用户不存在
        if(doctor == null) {
            log.info("doctor login failed,doctorAccount cannot match doctorPassword");
            return null;
        }

        //2.记录用户的登录态
        httpServletRequest.getSession().setAttribute(USER_LOGIN_STATE,doctor);

        System.out.println(doctor);
        return doctor;
    }

    @Override
    public Doctor serchDoctorByName(String doctorName){

        //查询医生是否存在
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("doctorName",doctorName);
        long count = doctorMapper.selectCount(queryWrapper);
        if(count==0){
            return null;
        }
        Doctor doctor = doctorMapper.selectOne(queryWrapper);
        return doctor;
    }
}




